package com.idbk.chargestation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityCertification;
import com.idbk.chargestation.adapter.DialogCarNumAdapter;

/**
 * 车牌号选择Dialog
 *
 * @author liangzw
 */
public class DialogCarNum extends Dialog implements OnItemClickListener {

    private Context context;
    private View mView;
    //	private GridView mGridView;//显示车牌地方列表
    private String[] name;
    private ActivityCertification activity;

    public DialogCarNum(Context context, ActivityCertification activity) {
        super(context);
        this.context = context;
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置dialog默认背景为透明
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setGravity(Gravity.BOTTOM);//设置Dialog显示在下方
            getWindow().setWindowAnimations(R.style.share_dialog);//设置显示动画
            LayoutInflater inflater = ((ActivityCertification) context).getLayoutInflater();
            mView = inflater.inflate(R.layout.dialog_car_num, null);
            setContentView(mView);
            //设置Dialog横向全屏,纵向匹配内容
            getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        }

        setupView();
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        this.dismiss();
        return super.onTouchEvent(event);
    }

    private void setupView() {
        name = context.getResources().getStringArray(R.array.car_number);
        GridView mGridView = (GridView) (mView).findViewById(R.id.car_number);
        DialogCarNumAdapter mAdapter = new DialogCarNumAdapter(context, name);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long arg3) {
        activity.setCarNum(name[position]);
        this.dismiss();
    }

}
